/*!
  @file memory_allocation.cpp
  @brief Brief description
  @author Kyohei Otsu <kyon@ac.jaxa.jp>
  @date April, 2013
*/

#include <cstdlib>

#include "png/libpng_helper.h"
#include "core/core.h"

using namespace std;

namespace png
{

int PNG::allocMemory(int width, int height, int channels) {
  data_ = (png_bytep*) malloc(sizeof(png_bytep) * height);
  if (!data_) goto alloc_error;
  for (int y = 0; y < height; y++) {
    data_[y] = (png_byte*) malloc(width * channels);
    if (!data_[y]) goto alloc_error;
  }
  memory_alloced_ = true;
  return 0;

alloc_error:
  ERROR("[png] memory allocation error");
  ABORT();
}

void PNG::releaseMemory()
{
  if (memory_alloced_) {
    for (int y = 0; y < height_; y++) {
      free(data_[y]);
    }
    free(data_);
    memory_alloced_ = false;
  }
}

} // end of namespace
